﻿//     Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Gateway.Console
{
    public static class ViewExtensions
    {
        const string DisabledAttribute = "disabled=\"true\"";

        public static MvcHtmlString Disable(this MvcHtmlString src, Func<bool> predicate)
        {
            if (predicate())
            {
                var html = src.ToString();
                html = html.Replace(">", " " + DisabledAttribute + ">");
                return new MvcHtmlString(html);
            }
            return src;
        }

        public static MvcHtmlString Submit(this HtmlHelper src, string id, string text, object htmlAttributes)
        {
            return src.Submit(id, text, ((IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
        }

        public static MvcHtmlString Submit(this HtmlHelper src, string id, string text, IDictionary<string, object> attributes)
        {
            var tagBuilder = new TagBuilder("input");
            tagBuilder.MergeAttributes(attributes);
            tagBuilder.MergeAttribute("type", "submit");
            tagBuilder.MergeAttribute("id", id, true);
            tagBuilder.MergeAttribute("value", text, true);
            return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
        }

        public static IDisposable BeginCollectionItem(this HtmlHelper html, string collectionName, string index)
        {
            int dummy;
            if (String.IsNullOrEmpty(index))
            {
                index = Guid.NewGuid().ToString();
            }
            else if (int.TryParse(index, out dummy))
            {
                if (dummy == 0)
                {
                    // Avoid duplicate 'new' item keys
                    index = Guid.NewGuid().ToString();
                }
                index = String.Format("{0}Key_{1}", collectionName, index);
            }
            // autocomplete="off" is needed to work around a very annoying Chrome behaviour whereby it reuses old values after the user clicks "Back", which causes the xyz.index and xyz[...] values to get out of sync.
            html.ViewContext.Writer.WriteLine(string.Format("<input type=\"hidden\" name=\"{0}.index\" autocomplete=\"off\" value=\"{1}\" />", collectionName, html.Encode(index)));

            return new HtmlFieldPrefixScope(html.ViewData.TemplateInfo, string.Format("{0}[{1}]", collectionName, index));
        }

        private class HtmlFieldPrefixScope : IDisposable
        {
            private readonly TemplateInfo templateInfo;
            private readonly string previousHtmlFieldPrefix;

            public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix)
            {
                this.templateInfo = templateInfo;

                previousHtmlFieldPrefix = templateInfo.HtmlFieldPrefix;
                templateInfo.HtmlFieldPrefix = htmlFieldPrefix;
            }

            public void Dispose()
            {
                templateInfo.HtmlFieldPrefix = previousHtmlFieldPrefix;
            }
        }
    }
}